﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 大彩串口更新图片
{
    public partial class Form1 : Form
    {
        string fileName = @"C:\Users\hyghy\Desktop\0.jpg";
        List<byte> serialPort1List = new List<byte>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM23";
            serialPort1.BaudRate = 115200;
            try
            {
                serialPort1.Open();
            }
            catch
            {
                MessageBox.Show("串口打开失败！");
            }

            Send_Picture();
            try
            {
                tbfilename.Text = fileName;
                FileNameStr = File.OpenRead(fileName);
                FileLen = FileNameStr.Length;
                textBox1.Text = (FileLen / 1024).ToString() + "K";
                FileReadByte = new byte[FileLen];
                for (int i = 0; i < FileLen; i++)
                {
                    FileReadByte[i] = (byte)FileNameStr.ReadByte();
                }
                FileNameStr.Close();
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
        }

        private void Send_Picture()
        {

            if (serialPort1.IsOpen)
            {
                byte[] b;
                b = new byte[] { 0xee, 0xf8, 0x2c, 0xf5, 0x34, 0x1d, 0x68, 0x99, 0x7b, 0xfb, 0x43, 0x47, 0x6d, 0x38, 0x23, 0x53, 0x0a, 0xe9, 0xeb, 0xd2, 0xff, 0xfc, 0xff, 0xff };
                serialPort1.Write(b, 0, b.Length);

                b = new byte[] { 0xee, 0xfe, 0xc0, 0x3e, 0xe0, 0xc1, 0xff, 0xfc, 0xff, 0xff };
                serialPort1.Write(b, 0, b.Length);
            }
        }
        bool flag = false;

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte[] buf = new byte[serialPort1.BytesToRead];
            serialPort1.Read(buf, 0, buf.Length);
            flag = true;
            //serialPort1List.AddRange(buf);

            //this.Invoke(new invokeDelegate(serialPort1_Task));
        }

        FileStream FileNameStr;
        byte[] FileReadByte;   //读取bin文件缓冲
        long FileLen;
        private void bchoosefile_Click(object sender, EventArgs e)
        {
            try
            {
                openfile.ShowDialog();//文件读取窗口
                fileName = openfile.FileName;//获取文件名
                tbfilename.Text = fileName;
                FileNameStr = File.OpenRead(fileName);
                FileLen = FileNameStr.Length;
                textBox1.Text = (FileLen / 1024).ToString() + "K";
                FileReadByte = new byte[FileLen];
                for (int i = 0; i < FileLen; i++)
                {
                    FileReadByte[i] = (byte)FileNameStr.ReadByte();
                }
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
            FileNameStr.Close();
        }

        private void Send_Data()
        {
            List<byte> byteL = new List<byte>();
            int fileCnt = 0;           
            byte[] btyeN = new byte[2052];
            int checksum = 0;
            
            for (int i = 0; i < FileReadByte.Length / 2048; i++)
            {
                btyeN[0] = (byte)i;
                btyeN[1] = (byte)~i;
                checksum = 0;
                for (int j = 0; j < 2048; j++)
                {
                    btyeN[2 + j] = FileReadByte[fileCnt++];
                }
                for (int j = 0; j < 2050; j++)
                {
                    checksum += btyeN[j];
                }
                checksum = ~checksum;
                btyeN[2050] = (byte)(checksum >> 8);
                btyeN[2051] = (byte)(checksum >> 0);
                serialPort1.Write(btyeN, 0, btyeN.Length);
                Thread.Sleep(500);
            }


            int num = FileReadByte.Length / 2048;
            checksum = 0;
            for (int i = 0; i < 2052; i++)
            {
                btyeN[i] = 0;
            }
            btyeN[0] = (byte)num;
            btyeN[1] = (byte)~num;

            for (int j = 0; j < FileReadByte.Length % 2048; j++)
            {
                btyeN[2 + j] = FileReadByte[fileCnt++];
            }

            for (int j = 0; j < 2050; j++)
            {
                checksum += btyeN[j];
            }
            checksum = ~checksum;
            btyeN[2050] = (byte)(checksum >> 8);
            btyeN[2051] = (byte)(checksum >> 0);
            serialPort1.Write(btyeN, 0, btyeN.Length);
            Thread.Sleep(500);

            byte[] b = new byte[] { 0xee, 0x07, 0x35, 0x5a, 0x53, 0xa5, 0xff, 0xfc, 0xff, 0xff };
            serialPort1.Write(b, 0, b.Length);
            Thread.Sleep(500);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();
                }
            }
            catch
            {
                MessageBox.Show("串口打开失败！");
                return;
            }
            List<byte> byteL = new List<byte>();
            byteL.Add(0xee);
            byteL.Add(0xfb);
            //BLOCKSIZE
            byteL.Add(0x08);
            byteL.Add(0x04);
            //BAUDRATE
            byteL.Add(0x00);
            byteL.Add(0x00);
            //BAUDRATE = 实际波特率/100
            byteL.Add(0x00);
            byteL.Add(0x00);
            //FILESIZE
            byteL.Add((byte)(FileLen >> 8));
            byteL.Add((byte)(FileLen >> 0));
            //FILENAME
            //byteL.AddRange(System.Text.Encoding.Default.GetBytes(@"3:\bin\image.bin"));
            byteL.AddRange(System.Text.Encoding.Default.GetBytes("3:/"+ System.IO.Path.GetFileName(fileName)));
            //byteL.AddRange(System.Text.Encoding.Default.GetBytes("3:/0.jpg"));
            byteL.Add(0x00);
            byteL.Add(0x00);
            byteL.Add(0x0f);
            byteL.Add(0xff);

            byteL.Add(0xff);
            byteL.Add(0xfc);
            byteL.Add(0xff);
            byteL.Add(0xff);
            byte[] b = byteL.ToArray();
            serialPort1.Write(b, 0, b.Length);
            Thread.Sleep(500);

            Send_Data();
            serialPort1.Close();
        }
    }
}
